import { ref} from 'vue'
import { defineStore } from 'pinia'  
import { cityApi ,weatherApi} from '@/api/api'

export const useLocalWeatherStore = defineStore('LocalWeather', () => {
  const LocalCity = ref({}); 
  const LocalWeather = ref([]); 
  const curWeather = ref({}); 
  
  const fetchLocalWeather = async () => {
    try {
      const res = await cityApi.getCurrent(); 
      LocalCity.value = res.data; 

      const [curWeatherRes, weatherRes] = await Promise.all([
        weatherApi.getCurWeather(LocalCity.value.adcode),
        weatherApi.getWeather(LocalCity.value.adcode)
      ]);

      curWeather.value = curWeatherRes.data.lives?.[0] || {};
      LocalWeather.value = weatherRes.data.forecasts?.[0]?.casts || [];
    } catch (error) {
      console.error('获取本地天气失败:', error);
      curWeather.value = {};
      LocalWeather.value = [];
    }
  };

  return { LocalCity, LocalWeather, curWeather, fetchLocalWeather }; 
});
